/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.easyedu.dictionary.explorer;

import eu.easyedu.dictionary.api.resources.Bundle;
import eu.easyedu.dictionary.explorer.nodes.DictionaryRootNode;
import java.io.Serializable;
import java.util.logging.Logger;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.util.ImageUtilities;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.openide.util.lookup.InstanceContent;

/**
 * Top component which displays something.
 */
final class DictionaryExplorerTopComponent extends TopComponent implements ExplorerManager.Provider {

    private static final long serialVersionUID = 1L;
    private static DictionaryExplorerTopComponent instance;
    /** path to the icon used by the component and its open action */
    private static final String ICON_PATH = Bundle.getResourcesPath() + "/dictionary16x16.png";
    private static final String PREFERRED_ID = "DictionaryExplorerTopComponent";
    private final ExplorerManager explorerManager = new ExplorerManager();
    private final InstanceContent content = new InstanceContent();

    private DictionaryExplorerTopComponent() {
        initComponents();
        associateLookup(ExplorerUtils.createLookup(explorerManager, getActionMap()));
        setName(Bundle.getMessage("CTL_DictionaryExplorerTopComponent"));
        setToolTipText(Bundle.getMessage("HINT_DictionaryExplorerTopComponent"));
        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
        explorerManager.setRootContext(DictionaryRootNode.getDictionaryRootNode());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        beanTreeScrollPane = new BeanTreeView();

        setLayout(new java.awt.BorderLayout());
        add(beanTreeScrollPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane beanTreeScrollPane;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized DictionaryExplorerTopComponent getDefault() {
        if (instance == null) {
            instance = new DictionaryExplorerTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the DictionaryExplorerTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized DictionaryExplorerTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(DictionaryExplorerTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof DictionaryExplorerTopComponent) {
            return (DictionaryExplorerTopComponent) win;
        }
        Logger.getLogger(DictionaryExplorerTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
    // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
    // TODO add custom code on component closing
    }

    /** replaces this in object stream */
    @Override
    public Object writeReplace() {
        return new ResolvableHelper();
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    final static class ResolvableHelper implements Serializable {

        private static final long serialVersionUID = 1L;

        public Object readResolve() {
            return DictionaryExplorerTopComponent.getDefault();
        }
    }

    public ExplorerManager getExplorerManager() {
        return explorerManager;
    }
}
